package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.check.checkaddrrecord;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.CheckKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.ConfigKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.file.FileStrUtil;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

@Service
@Slf4j
public class CheckaddrRecordService extends BaseService {

	private static final Logger logger = LoggerFactory.getLogger(CheckaddrRecordService.class);


	/**
	 * 分区位置详情
	 * @param
	 * @return
	 */
	public RespEntity checkaddrRecordDetail(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"checkJava","checkaddrRecordDetail","checkaddrRecord");
	}

	/**
	 * 分区位置详情：app(带分页)
	 * @param
	 * @return
	 */
	public RespEntity checkaddrRecordDetailForApp(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"checkJava","checkaddrRecordDetailForApp","checkaddrRecord");
	}

	/**
	 * 巡检巡查详情
	 * @param
	 * @return
	 */
	public RespEntity checkDetail(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","checkDetail","checkaddrRecord");
	}

	/**
	 * 计划详情
	 * @param
	 * @return
	 */
	public RespEntity planDetail(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"checkJava","planDetail","checkaddrRecord");
	}

	/**
	 * 检查记录添加
	 * @param
	 * @return
	 */
	public RespEntity add(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"checkJava","add","checkaddrRecord");
	}

	/**
	 * 巡检地址记录列表
	 * @param
	 * @return
	 */
	public RespEntity checkaddrRecordList(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"checkJava","checkaddrRecordList","checkaddrRecord");
	}

	/**
	 * 巡检记录下载
	 * @param httpServletRequest
	 * @return
	 * @throws Exception
	 */
	public ResponseEntity<byte[]> getCheckUpload(Map<String,Object> map, HttpServletRequest httpServletRequest ) throws Exception {
		String ip = ConfigKit.getStr("checkJava","config");
		String methodPath = CheckKit.getStr("getCheckUpload","checkaddrRecord");
		String domain = ip + methodPath;
		Set<String> set = map.keySet();
		if (set.size()>0){
			domain=domain+"?";
			for (String str : set){
				if(map.get(str)!=null && StrUtil.isNotBlank(map.get(str).toString())){
					domain=domain+str+"="+ HttpUtil.encodeUtf8(map.get(str).toString())+"&";
				}else{
					domain=domain+str+"="+map.get(str)+"&";
				}
			}
			domain=domain.substring(0,domain.length()-1);
		}
		String filepath = "巡检记录.xlsx";
		byte[] btImg = FileStrUtil.getImageFromNetByUrl(domain);
		HttpHeaders headers = new HttpHeaders();
		// 下载显示的文件名，解决中文名称乱码问题
		String downloadFielName = new String(filepath.getBytes("UTF-8"), "iso-8859-1");
		// 通知浏览器以attachment（下载方式）打开图片
		headers.setContentDispositionFormData("attachment", downloadFielName);
		// application/octet-stream ： 二进制流数据（最常见的文件下载）。
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(btImg, headers, HttpStatus.CREATED);
	}
}
